const {
    Workflow,
    sequelize
} = require('../models')

module.exports = {
    async create(id) {
        return await Workflow.create({ id })
    },


    /**
     * workflow list
     * @param  {Object} where condition
     * @return {Promise}
     */
    async pagination(pagination, condition) {
        return await Workflow.$pagination(pagination, condition)
    },


    /**
     * workflow precheck
     * @param  {String} id    workflow id
     * @param  {Object} data  precheck object
     * @return {Promise}
     */
    async preCheck(user_id) {
        let workflow = await Workflow.findOne({
            where: {
                locked_by: user_id,
                step: 'precheck'
            }
        })

        if (workflow) return workflow

        workflow = await Workflow.findOne({
            where: {
                step: 'preparation',
                locked_by: null
            },
            order: [
                ['id', 'DESC']
            ]
        })

        if (workflow) {
            workflow.step = 'precheck'
            workflow.locked_by = user_id
            workflow.locked_at = new Date()
            await workflow.save()
        }

        return workflow
    },


    /**
     * workflow precheck pass
     * @param  {String} id    workflow id
     * @param  {Object} data  precheck object
     * @return {Promise}
     */
    async preCheckPass(id, user_id, data) {
        const workflow = await Workflow.findById(id)

        if (!workflow) return

        // @todo
        // if ( workflow.step !== 'precheck' ) {
        //     const error = new Error('Error')
        //     error.status = 400
        //     error.message = 'WorkflowPrechecked'
        //     throw error
        // }

        workflow.step = 'precheck_pass'
        workflow.locked_by = null
        workflow.locked_at = null
        workflow.pre_check_at = new Date()
        workflow.pre_check_by = user_id
        await workflow.save()
        return workflow
    },


    /**
     * workflow precheck reject
     * @param  {String} id    workflow id
     * @param  {Object} data  precheck object
     * @return {Promise}
     */
    async preCheckReject(id, user_id, data) {
        const workflow = await Workflow.findById(id)

        if (!workflow) return

        /*
        if (workflow.step !== 'precheck') {
            const error = new Error('Error')
            error.status = 400
            error.message = 'WorkflowPrechecked'
            throw error
        }
        */

        workflow.step = 'precheck_reject'
        workflow.locked_by = null
        workflow.locked_at = null
        workflow.pre_check_at = new Date()
        workflow.pre_check_by = user_id
        await workflow.save()
        return workflow
    },


    /**
     * workflow complete
     * @param  {String} id    workflow id
     * @return {Workflow}
     */
    async complete(user_id) {

        let workflow = await Workflow.findOne({
            where: {
                locked_by: user_id,
                step: 'complete'
            }
        })

        if (!workflow) {
            workflow = await Workflow.find({
                where: {
                    step: 'precheck_pass',
                    locked_by: null
                }
            })

            if (workflow) {
                workflow.step = 'complete'
                workflow.locked_by = user_id
                workflow.locked_at = new Date()
                await workflow.save()
            }
        }

        return workflow
    },


    /**
     * workflow complete pass
     * @param  {String} id    workflow id
     * @return {Workflow}
     */
    async completePass(id, user_id, data) {
        const workflow = await Workflow.findById(id)

        if (!workflow) return

        if (user_id !== workflow.locked_by) {
            console.log('@TODO: `locked_by`')
        }

        workflow.step = 'complete_pass'
        workflow.locked_by = null
        workflow.locked_at = null
        await workflow.save()

        return workflow
    },


    /**
     * workflow postcheck
     * @param  {String} id    workflow id
     * @return {Workflow}
     */
    async postCheck(user_id) {
        let workflow = await Workflow.find({
            where: {
                locked_by: user_id,
                step: 'postcheck'
            }
        })

        if (!workflow) {
            workflow = await Workflow.find({
                where: {
                    step: 'complete_pass',
                    locked_by: null
                }
            })

            if (workflow) {
                workflow.step = 'postcheck'
                workflow.locked_by = user_id
                workflow.locked_at = new Date()
                await workflow.save()
            }
        }

        return workflow
    },


    /**
     * workflow postcheck pass
     * @param  {String} id    workflow id
     * @param  {Object} data  postcheck object
     * @return {Workflow}
     */
    async postCheckPass(id, user_id) {
        const workflow = await Workflow.findById(id)

        if (!workflow) return

        if (workflow.step !== 'postcheck') {
            console.log('@TODO STEP')
        }

        if (workflow.locked_by !== user_id) {
            console.log('@TODO USER')
        }

        workflow.step = 'postcheck_pass'
        workflow.locked_by = null
        workflow.locked_at = null
        workflow.post_check_at = new Date()
        workflow.post_check_by = user_id
        await workflow.save()
        return workflow
    },


    /**
     * workflow complete
     * @param  {String} id    workflow id
     * @return {Workflow}
     */
    async postCheckReject(id, user_id) {
        const workflow = await Workflow.findById(id)

        if (!workflow) return

        workflow.step = 'postcheck_reject'
        workflow.locked_by = null
        workflow.locked_at = null
        workflow.post_check_at = new Date()
        workflow.post_check_by = user_id
        await workflow.save()
        return workflow
    },
}